package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.content.TbContentDto;
import com.bole.dto.img.TbCarImgDto;
import com.bole.dto.user.UserDto;
import com.bole.feign.CarFeign;
import com.bole.feign.TbCarImgFeign;
import com.bole.feign.reception.ContentFeign;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentFeign contentFeign;

    @Autowired
    private CarFeign carFeign;

    @Autowired
    private TbCarImgFeign carImgFeign;

    //查询轮播图
    @RequestMapping("selectRotation")
    @ResponseBody
    @ApiImplicitParam(name = "contentTypeId", value = "关联类型ID", required = true, dataTypeClass = Long.class)
    public Result selectRotation(@RequestParam Long contentTypeId){
        List<TbContentDto> tbContentDtos = contentFeign.selectRotation(contentTypeId);
        return Result.response(ResultEnum.OK).setData(tbContentDtos);
    }

    //千人千面
    @PostMapping("selectIntelligence")
    @ResponseBody
    @ApiOperation(value = "查询千人千面",notes = "查询千人千面")
    public Result selectIntelligence(){
        //判断用户是否登录
        Object principal = SecurityUtils.getSubject().getPrincipal();
        System.out.println(principal);
        UserDto user = (UserDto)principal;
        if(user == null || "".equals(user)){
           //未登录，走默认
            List<TbCarDto> carInfo = carFeign.getCarInfoSix();
            return  Result.response(ResultEnum.OK).setData(carInfo);
        }else{
            //登录
            long id = user.getId();
            List<TbCarDto> carDtos = contentFeign.selectIntelligence(id);
            return Result.response(ResultEnum.OK).setData(carDtos);
        }
    }
    //查询图片
    @PostMapping("findAllByCarId")
    @ResponseBody
    @ApiOperation(value = "通过汽车id查询",notes = "通过汽车id查询")
    @ApiImplicitParam(name = "carId", value = "汽车id", required = true, dataTypeClass = Long.class)
    public Result findAllByCarId(@RequestParam Long carId){
        TbCarImgDto allByCarId = carImgFeign.findByCarId(carId);
        return Result.response(ResultEnum.OK).setData(allByCarId);
        }
}
